package com.billy.android.loadingstatusview;

import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.billy.android.loading.DefaultCustomAdapter;
import com.billy.android.loading.Gloading;
import com.billy.android.loadingstatusview.wrapactivity.SpecialActivity;

/**
 * base activity
 *
 * @author billy.qi
 * @since 19/3/19 21:15
 */
public abstract class BaseActivity extends Activity {

    protected Gloading.Holder mHolder;

    /**
     * make a Gloading.Holder wrap with current activity by default
     * override this method in subclass to do special initialization
     *
     * @see SpecialActivity
     */
    protected void initLoadingStatusViewIfNeed() {
        if (mHolder == null) {
            //bind status view to activity root view by default
//            mHolder = Gloading.from(new DefaultCustomAdapter()).wrap(this).withRetry(new Runnable() {
             mHolder = Gloading.getDefault().wrap(this).withRetry(new Runnable() {
                @Override
                public void run() {
                    onLoadRetry();
                }
            }).setLoadingView(getLoadingView())
              .setEmptyView(getDefaultStatusView(R.drawable.icon_empty, "没有数据", "", null))
              .setErrorView(getDefaultStatusView(R.drawable.icon_failed, "加载失败，点击重试", "重新加载", new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      onLoadRetry();
                  }
              }));
        }
    }

    protected void onLoadRetry() {
        // override this method in subclass to do retry task
    }

    public void showLoading() {
        initLoadingStatusViewIfNeed();
        mHolder.showLoading();
    }

    public void showLoadSuccess() {
        initLoadingStatusViewIfNeed();
        mHolder.showLoadSuccess();
    }

    public void showLoadFailed() {
        initLoadingStatusViewIfNeed();
        mHolder.showLoadFailed();
    }

    public void showEmpty() {
        initLoadingStatusViewIfNeed();
        mHolder.showEmpty();
    }

    public void showCustomView(View view) {
        initLoadingStatusViewIfNeed();
        mHolder.showView(view);
    }


    protected View getLoadingView() {
        return LayoutInflater.from(this).inflate(R.layout.view_loading, null, false);
    }

    protected View getDefaultStatusView(int resId, String msg, String retryBtn, View.OnClickListener retryListener) {
        View customView = LayoutInflater.from(this).inflate(R.layout.view_status_custom, null, false);
        if (retryListener != null) {
            customView.findViewById(R.id.btnRetry).setOnClickListener(retryListener);
        }
        if (resId != -1) {
            ((ImageView) customView.findViewById(R.id.image)).setImageResource(resId);
        }
        if (!TextUtils.isEmpty(msg)) {
            ((TextView) customView.findViewById(R.id.text)).setText(msg);
        }
        ((Button) customView.findViewById(R.id.btnRetry)).setVisibility(TextUtils.isEmpty(retryBtn) ? View.GONE : View.VISIBLE);
        if (!TextUtils.isEmpty(retryBtn)) {
            ((Button) customView.findViewById(R.id.btnRetry)).setText(retryBtn);
        }
        return customView;
    }

}
